import { ElMessage } from 'element-plus'

/*
        //// 有需求在使用
// 设置 localStorage 中 name 对应数据 data，并设置有效期为 pExpires 毫秒
export function setLocal(name: string, data: IObject<any>, pExpires = 1000 * 60 * 60 * 24 * 365 * 100): void {
    const item: ILocalStore<any> = {
        data,
        startTime: Date.now(),
        expires: pExpires,
    };
    localStorage.setItem(name, JSON.stringify(item))
}

// 获取 localStorage 中 name 对应数据，并校验有效性
export async function getLocal(name: string): Promise<ILocalStore<any>> {
    return new Promise((resolve, reject) => {
        const local = JSON.parse(localStorage.getItem(name) as string)
        if (local && local.startTime + local.expires < Date.now())
            reject(`${name}已超过有效期`)
        resolve(local)
    })
}

// 二次编码url
export function decode(url: string): string {
    return decodeURIComponent(decodeURIComponent(url))
}

// 二次解码url
export function encode(url: string): string {
    return encodeURIComponent(encodeURIComponent(url))
}
*/

// o: 表示前端传来的消息字符串
export function showElMessage(err: any, level: 'success' | 'error' | 'info' | 'warning', o: Boolean = false): void {
    ElMessage({
        message: o ? err : err.msg || err.response?.data.msg || 'Error',
        type: level,
        duration: 2 * 1000
    });
}